package com.qf.bigdata.view.web.controller.api;


import com.qf.bigdata.view.web.service.DeviceService;
import com.qf.data.view.core.model.exception.DevcieException;
import com.qf.data.view.core.model.request.AddDeviceRequest;
import com.qf.data.view.core.model.request.UpdateDeviceRequest;
import com.qf.data.view.core.model.response.DeviceSelectResponse;
import com.qf.data.view.core.model.result.ResultModel;
import org.apache.dubbo.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author q
 * @version V1.0
 * @date 2022/01/04
 **/
@RestController
@RequestMapping("/device")
public class DeviceAPIController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 根据设备key查询设备
     * @param key
     * @return
     */
    @GetMapping("/info/{key}")
    ResultModel<DeviceSelectResponse> getDeviceByKey(@PathVariable String key) {

        try {
            if (StringUtils.isBlank(key)) {
                throw new DevcieException("param error...");
            }
            return deviceService.getDeviceByKey(key);
        } catch (DevcieException e) {
            e.printStackTrace();
            return ResultModel.error(e.getMessage());
        }
    }

    /**
     * 添加设备
     * @param addDeviceRequest
     * @return
     */
    @PostMapping("/insert")
    ResultModel addDevice(@RequestBody AddDeviceRequest addDeviceRequest) {
        try {
            if (Objects.nonNull(addDeviceRequest)) {
                return deviceService.addDevice(addDeviceRequest);
            }
            throw new DevcieException("param error...");
        } catch (DevcieException e) {
            e.printStackTrace();
            return ResultModel.error(e.getMessage());
        }
    }

    /**
     * 修改设备
     * @param updateDeviceRequest
     * @return
     */
    @PutMapping("/updateByDeviceId")
    ResultModel updateByDeviceId(@RequestBody UpdateDeviceRequest updateDeviceRequest){
        try {
            if (Objects.nonNull(updateDeviceRequest)) {
                return deviceService.updateByDeviceId(updateDeviceRequest);
            }
            throw new DevcieException("param error...");
        } catch (DevcieException e) {
            e.printStackTrace();
            return ResultModel.error(e.getMessage());
        }
    }

}
